<template>
  <CommonPage>
    <template #action>
      <NButton mr-5 type="primary" size="small" @click="handleExportAll">
        <i class="i-fe:cloud-lightning mr-4 text-16" />
        导出全部日志
      </NButton>
    </template>

    <MeCrud
      ref="$table"
      v-model:query-items="queryItems"
      :scroll-x="1200"
      :columns="columns"
      :get-data="api.read"
    >
      <MeQueryItem label="用户名" :label-width="50">
        <n-input
          v-model:value="queryItems.username"
          type="text"
          placeholder="请输入用户名"
          clearable
        />
      </MeQueryItem>

      <MeQueryItem label="操作类型" :label-width="70">
        <n-input
          v-model:value="queryItems.method"
          type="text"
          placeholder="请输入操作类型"
          clearable
        />
      </MeQueryItem>

      <MeQueryItem label="状态码" :label-width="50">
        <n-input
          v-model:value="queryItems.response_code"
          type="text"
          placeholder="请输入状态码"
          clearable
        />
      </MeQueryItem>

      <MeQueryItem label="操作IP">
        <n-input v-model:value="queryItems.ip" type="text" placeholder="请输入操作IP" clearable />
      </MeQueryItem>
    </MeCrud>

    <ViewDetails ref="modalRef" />
  </CommonPage>
</template>

<script setup>
import { CommonPage, MeCrud, MeQueryItem } from '@/components'
import { useCrud } from '@/composables'
import { NButton } from 'naive-ui'
import { useTemplateRef } from 'vue'
import api from './api'
import ViewDetails from './components/ViewDetails.vue'

defineOptions({ name: 'OperLog' })

const $table = useTemplateRef('$table')
const queryItems = ref({})

onMounted(() => {
  $table.value?.handleSearch()
})

const { handleDelete } = useCrud({
  name: '操作日志',
  doDelete: api.delete,
  refresh: () => $table.value?.handleSearch(),
})

const columns = [
  { title: 'ID', key: 'id', width: 80 },
  { title: '操作类型', key: 'method' },
  { title: 'IP', key: 'ip' },
  { title: '操作地点', key: 'ip_location' },
  { title: '操作人员', key: 'username' },
  { title: '操作名称', key: 'service_name' },
  { title: '响应代码', key: 'response_code' },
  { title: '操作时间', key: 'created_at' },
  {
    title: '操作',
    key: 'actions',
    align: 'right',
    fixed: 'right',
    hideInExcel: false,
    render(row) {
      return [
        h(
          NButton,
          {
            size: 'small',
            text: true,
            type: 'primary',
            style: 'margin-left: 12px;',
            onClick: () => handleView(row),
          },
          {
            default: () => '详情',
            icon: () => h('i', { class: 'i-fe:aperture text-14' }),
          },
        ),

        h(
          NButton,
          {
            size: 'small',
            text: true,
            type: 'primary',
            style: 'margin-left: 12px;',
            onClick: () => handleDelete(row.id, row.router),
          },
          {
            default: () => '删除',
            icon: () => h('i', { class: 'i-material-symbols:delete-outline text-14' }),
          },
        ),
      ]
    },
  },
]

const modalRef = useTemplateRef('modalRef')
function handleView(row) {
  modalRef.value?.handleOpen({
    title: '操作日志详细信息',
    row,
  })
}

function handleExportAll() {
  $dialog.success({
    title: '导出全部数据',
    content: '导出全部数据，可根据上方表单条件导出',
    positiveText: '导出',
    negativeText: '取消',
    onPositiveClick: async () => {
      const { message } = await api.exportOperLogs(queryItems.value)
      $message.success(message)
    },
  })
}
</script>
